package com.example.beson.androiddemo;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.beson.androiddemo.login.mvp.model.UserInfo;
import com.example.beson.androiddemo.login.mvp.presenter.LoginPresenter;
import com.example.beson.androiddemo.login.mvp.view.ILoginView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 实现 ILoginView 接口，并与 LoginPresenter 关联
 */
public class MVPLoginActivity extends AppCompatActivity implements ILoginView{
    @BindView(R.id.login_progressbar)
    ProgressBar loginProgressbar;
    @BindView(R.id.login_title)
    TextView loginTitle;
    @BindView(R.id.login_phone)
    EditText loginPhone;
    @BindView(R.id.login_pwd)
    EditText loginPwd;
    @BindView(R.id.login_submit)
    Button loginSubmit;
    private LoginPresenter mLoginPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvplogin);
        ButterKnife.bind(this);
        mLoginPresenter=new LoginPresenter(this);
        loginTitle.setText("MVP  登录");
    }

    @OnClick(R.id.login_submit)
    public void onViewClicked(View view ) {
        switch (view.getId()){
            case R.id.login_submit:
                String phoneNum=loginPhone.getText().toString().trim();
                String password=loginPwd.getText().toString().trim();
                if(mLoginPresenter.checkUserNameLogin(phoneNum,password)){
                    mLoginPresenter.phoneLogin(phoneNum,password);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void showLoading() {
        loginProgressbar.setVisibility(View.VISIBLE);
    }

    @Override
    public void dismissLoading() {
        loginProgressbar.setVisibility(View.GONE);
    }

    @Override
    public void showMsg(String msg) {
        Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    public Context getContext() {
        return getApplicationContext();
    }

    @Override
    public void loginSuccess(UserInfo userinfo) {
        showMsg("登录成功   "   +userinfo.getPhonenum());
    }

    @Override
    public void loginFailed(UserInfo userinfo) {
        showMsg("登录失败   "   +userinfo.getPhonenum());
    }

    @Override
    public void phoneError(String errorMsg) {
        showMsg(errorMsg);
    }

    @Override
    public void passwordError(String errorMsg) {
        showMsg(errorMsg);
    }
}
